package com.nxxskj.alcohol.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.nxxskj.alcohol.bean.Record;
import com.nxxskj.alcohol.bean.Role;
import com.nxxskj.alcohol.bean.User;
import com.nxxskj.alcohol.service.DeptService;
import com.nxxskj.alcohol.service.RecordService;
import com.nxxskj.alcohol.service.RoleService;
import com.nxxskj.alcohol.service.UserService;
import com.nxxskj.common.model.RestResponse;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Controller
@RequestMapping("/apply")
public class ApplyController {
    @Autowired
    private UserService userService;
    @Autowired
    private RoleService roleService;
    @Autowired
    private RecordService recordService;
    @Autowired
    private DeptService deptService;

    @RequestMapping("applyList.do")
    public String applyList(HttpServletRequest request, HttpSession session) {
        User user = (User) session.getAttribute("user");
        LambdaQueryWrapper<Record> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Record::getDrinkUid, user.getId());
        List<Record> records = recordService.list(wrapper);
        request.setAttribute("list", records);
        return "applylist";
    }


    @RequestMapping("dealList.do")
    public String dealList(HttpServletRequest request, HttpSession session) {
        User user = (User) session.getAttribute("user");

       List<Record> list= recordService.getdealListByUser(user);
        LambdaQueryWrapper<Record> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(Record::getReportUid, user.getId());
        wrapper.eq(Record::getReportResult, "0");//待处理
        request.setAttribute("applyList", list.stream().filter(record -> record.getReportResult().equals("0")).collect(Collectors.toList()));

        wrapper = new LambdaQueryWrapper<>();
        wrapper.like(Record::getReportUid, user.getId());
        wrapper.eq(Record::getReportResult, "1");//同意的列表
        request.setAttribute("passList", list.stream().filter(record -> record.getReportResult().equals("1")).collect(Collectors.toList()));

        wrapper = new LambdaQueryWrapper<>();
        wrapper.like(Record::getReportUid, user.getId());
        wrapper.eq(Record::getReportResult, "-1");//拒绝的列表
        request.setAttribute("rejectList", list.stream().filter(record -> record.getReportResult().equals("-1")).collect(Collectors.toList()));
        return "deallist";
    }

    @RequestMapping("toDealApply.do")
    public String toDealApply(HttpServletRequest request, String recodeId) {
        Record record = recordService.getById(recodeId);
        request.setAttribute("record", record);
        return "dealApply";
    }

    @RequestMapping("doDealApply.do")
    public String doDealApply(String id, String reportResult, String reportExplain, HttpSession session) {
        User user = (User) session.getAttribute("user");
        LambdaUpdateWrapper<Record> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(Record::getReportUid, user.getId());
        wrapper.set(Record::getReportPersonName, user.getName());
        wrapper.set(Record::getReportRole, user.getRole());
        wrapper.set(Record::getReportTime, new Date());
        wrapper.set(Record::getReportResult, reportResult);
        wrapper.set(Record::getReportExplain, reportExplain);
        wrapper.eq(Record::getId, id);
        recordService.update(wrapper);
        return "redirect:/apply/dealList.do";
    }

    @ResponseBody
    @RequestMapping("/saveApply.do")
    public RestResponse saveApply(HttpSession session, Record record) throws Exception {


        Date start = DateUtils.parseDate(record.getDrinkStartTimes().replace("T"," ")+":00", new String[]{"yyyy-MM-dd HH:mm:ss"});
        Date end = DateUtils.parseDate(record.getDrinkEndTimes().replace("T"," ")+":00", new String[]{"yyyy-MM-dd HH:mm:ss"});

        if(end.getTime()-start.getTime()<0)
        {
            return RestResponse.error("饮酒开始时间比结束时间要晚，请重新输入");
        }

        //1、先查出谁可以处理我的申请
        User user = (User) session.getAttribute("user");


        if(record.getEmergencyPhone().equals(user.getPhone()))
        {
            return RestResponse.error("紧急联系电话不能和本人电话一样，请重新输入");
        }
        record.setDrinkApplyTime(new Date());
        record.setDrinkRole(user.getRole());
        record.setDrinkPerson(user.getName());
        record.setDrinkUid(user.getId());
        record.setDrinkDept(user.getDeptCode());
        record.setReportDept(user.getDeptCode());
        record.setDrinkStartTime(start);
        record.setDrinkEndTime(end);

        if (StringUtils.isBlank(record.getId())) {
            record.setDrinkApplyTime(new Date());
        }
        boolean flag = recordService.saveOrUpdate(record);
        if (flag) {
            return RestResponse.success();
        } else {
            return RestResponse.error("保存失败");
        }

    }

    @RequestMapping("/toApply.do")
    public String toApply(HttpServletRequest request) {
        String action = request.getParameter("action");
        String id = request.getParameter("id");
        Record record = new Record();
        if (StringUtils.isNotBlank(id)) {
            record = recordService.getById(id);
        }
        request.setAttribute("record", record);
        request.setAttribute("action", action);
        return "apply";
    }

}
